﻿
using DetectingKinectSencor.Tools;
using System.Data.SQLite;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DetectingKinectSencor._40_Tools;


namespace DetectingKinectSencor.BodyLanguage
{
    class ViewingDirectionManager
    {
        private Skeleton skeleton;

        private Stopwatch counterTillLookingAwayStarts;
        private Stopwatch counterHowLongLookedAway;

        private bool lookedAway;
        private int timesLookedAwayID;
        private Vector3 leftShoulderPosition;
        private Vector3 leftHandPosition;
        private Vector3 rightShoulderPosition;
        private Vector3 rightHandPosition;
        private float leftDistance;
        private float rightDistance;
        private SQLiteConnection databaseConnection;
        private SQLiteCommand sqliteCommand;
        public DateTime startedCaptureTime;
        private DateTime lookingAwayTimePoint;

        private TimeSpan timeSpanlookedAway;

        public ViewingDirectionManager(SQLiteConnection databaseConnection)
        {
            this.databaseConnection = databaseConnection;

            timesLookedAwayID = 0;

            // create a new SQL command:
            sqliteCommand = databaseConnection.CreateCommand();

            counterTillLookingAwayStarts = new Stopwatch();
            counterHowLongLookedAway = new Stopwatch();
        }
            

        internal void doWork(object skeleton)
        {
             this.skeleton = (Skeleton) skeleton;

            leftShoulderPosition = Vector3.ToVector3(this.skeleton.Joints.Where(j => j.JointType ==
            JointType.ShoulderLeft).First().Position);

            leftHandPosition = Vector3.ToVector3(this.skeleton.Joints.Where(j => j.JointType ==
            JointType.HandLeft).First().Position);

            rightShoulderPosition = Vector3.ToVector3(this.skeleton.Joints.Where(j => j.JointType ==
            JointType.ShoulderRight).First().Position);

            rightHandPosition = Vector3.ToVector3(this.skeleton.Joints.Where(j => j.JointType ==
            JointType.HandRight).First().Position);

            leftDistance = leftShoulderPosition.Z;
            rightDistance = rightShoulderPosition.Z;



            if (Math.Abs(leftDistance - rightDistance) > XMLproperties.offsetViewingDirection)
            {
                if (counterTillLookingAwayStarts.IsRunning)
                {
                    if (counterTillLookingAwayStarts.Elapsed.Seconds > XMLproperties.secondsLookedAwayDetected)
                    {
                        lookedAway = true;
                    }
                }
                else
                {
                    lookingAwayTimePoint = DateTime.Now;
                    counterTillLookingAwayStarts.Start();
                }
            }
            else
            {
                if (lookedAway)
                {
                    try
                    {

                        timeSpanlookedAway = DateTime.Now - lookingAwayTimePoint;

                        timesLookedAwayID++;

                        // Saved the detection into the sqlite DB, timespan is how long speaker stood on the same spot before started moving
                        sqliteCommand.CommandText =
                            "INSERT INTO looked_away (capturedTime, timepoint, timespanMinute, timespanSec) VALUES ('" + startedCaptureTime + "', '"
                                                                                                                       + lookingAwayTimePoint + "', "
                                                                                                                       + timeSpanlookedAway.Minutes + ","
                                                                                                                       + timeSpanlookedAway.Seconds
                                                                                                                       + ");";
                        sqliteCommand.ExecuteNonQuery();

                        // plus secondsTillLookedAwayDetected second because this is the time, till the counter starts
                        Console.WriteLine("times looked away: " + timesLookedAwayID
                                                                + " how long: "
                                                                + timeSpanlookedAway.Minutes
                                                                + ":" + timeSpanlookedAway.Seconds);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    lookedAway = false;

                }

                if(counterTillLookingAwayStarts.IsRunning)
                    counterTillLookingAwayStarts.Reset();
            }
        }
    }
}
